package com.h819.core.net;

import it.sauronsoftware.ftp4j.FTPClient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import org.apache.log4j.Logger;

import com.h819.core.db.MyDbUtils;

/**
 * @Title: MyNetMonitorUtils.java
 * @Description: TODO(监控系统服务)
 * @author H819
 * @date 2010-12-20
 * @version V1.0
 */
public class MyNetMonitorUtils {

	private static Logger log = Logger.getLogger(MyNetMonitorUtils.class);

	/**
	 * 仅检测服务是否可用，不验证用户名和密码
	 * 
	 * @param hostIp
	 *            The Host name or ip
	 * @param port
	 *            The port number
	 * @param timeout
	 *            判定超时时长，单位：秒
	 * @return
	 */
	public static boolean isNetServiceOpen(String hostIp, int port, int timeout) {

		boolean isOpen = true;
		Socket soc = null;
		try {

			soc = new Socket();
			soc.connect(new InetSocketAddress(hostIp, port), timeout * 1000);
			log.info("ip :" + soc.getInetAddress());
			log.info("port :" + soc.getPort());
			log.info("isConnected :" + soc.isConnected());

		} catch (IOException e) {
			isOpen = false;
			// e.printStackTrace();
		} finally {
			if (soc != null) {
				try {
					soc.close();
				} catch (IOException ignored) {
				}
			}
		}

		return isOpen;
	}

	/**
	 * 仅检测服务是否可用，不验证用户名和密码，超长时间默认为 1 秒。
	 * 
	 * @param hostIp
	 * @param port
	 * @return
	 */
	public static boolean isNetServiceOpen(String hostIp, int port) {

		return isNetServiceOpen(hostIp, port, 1);

	}

	/**
	 * 户名、密码登录，检查 ftp 服务是否可用
	 * 
	 * @param hostIp
	 * @param port
	 * @param username
	 * @param password
	 * @return
	 */
	public static boolean isFtpInServiceOpen(String hostIp, int port,
			String username, String password) {

		// 如果 ftp 不可用，改方法会用比较长的时间进行尝试连接，比较耗时，以后重新实现这个方法。

		FTPClient client = new FTPClient();
		try {
			client.connect(hostIp, port);
			client.login(username, password);
			return client.isConnected();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 用户名、密码登录，检测数据库服务是否可用
	 * 
	 * @param driverName
	 * @param connectionURL
	 * @param username
	 * @param password
	 * @return
	 */
	public static boolean isDBInServiceOpen(String driverName,
			String connectionURL, String username, String password) {

		// "com.mysql.jdbc.Driver",
		// "jdbc:mysql://localhost:33067/test",
		// "root",
		// "123456"
		return MyDbUtils.getInstanceDefault().isValid(driverName,
				connectionURL, username, password);

	}


	/**
	 * @param args
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// out.println(MyNetMonitorUtils.isNetServiceOpen("194.151.77.244",
		// 1723, 1));

		 MyNetMonitorUtils.isNetServiceOpen("129.9.100.11", 1521);


		//
		// out.println(SystemServiceMonitor.isNetServiceOpen("www.baidu.com",
		// 80));
		//
		// out.println(SystemServiceMonitor.isFtpInServiceOpen("192.168.1.5",
		// 21,
		// "", ""));
		// out.println(SystemServiceMonitor.isDBInServiceOpen(
		// "oracle.jdbc.driver.OracleDriver",
		// "jdbc:oracle:thin:@129.9.200.235:1521:orcl", "standard",
		// "standard"));
		//
		// out.println(SystemServiceMonitor.isDBInService("com.mysql.jdbc.Driver",
		// "jdbc:mysql://localhost:3306/test", "root", "123456"));

	}
}